const TOKEN_KEY = 'TOKEN_KEY'
const USER_INFO = 'USER_INFO'

export function getToken() {
  return localStorage.getItem(TOKEN_KEY)
}

export function setToken(token) {
  return localStorage.setItem(TOKEN_KEY, token)
}

export function removeToken() {
  return localStorage.removeItem(TOKEN_KEY)
}

export function getUserInfo() {
  let userInfo = localStorage.getItem(USER_INFO)
  return userInfo ? JSON.parse(userInfo) : {}
}

export function setUserInfo(userData) {
  return localStorage.setItem(USER_INFO, JSON.stringify(userData))
}

export function removeUserInfo() {
  return localStorage.removeItem(USER_INFO)
}
/**
 * 微信授权
 * config:
 *  appId - 公众号appId
 *  REDIRECT_URI - 回调域名，授权后跳转的地址
 *  SCOPE - 授权类型，snsapi_userinfo／snsapi_base
 */
export function auth () {
  /* eslint-disable */
  // let
  //   appid = 'wx37a337ef80850a79',
  //   oldURL = window.location.href,
  //   REDIRECT_URI = oldURL.split('#')[0],
  //   SCOPE = 'snsapi_userinfo',
  //   url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + REDIRECT_URI + '&response_type=code&scope=' + SCOPE + '&state=1#wechat_redirect'
  // window.location.href = url
}

/**
 * 获取授权后的code
 * @returns {*}
 */
export function getCode () {
  let url = window.location.search
  if (url === '') {
    return false
  }
  let code = url.match(/^\?code=(.+)&/)[1]
  return code
}